package com.mes.inventory.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.provider.Page;
import com.mes.basicdata.domain.Operation;
import com.mes.common.service.BasicService;
import com.mes.common.util.SysUtil;
import com.mes.inventory.dao.iface.IInvCheckDao;
import com.mes.inventory.dao.iface.IInvCheckDetailDao;
import com.mes.inventory.dao.iface.IInvIODetailDao;
import com.mes.inventory.domain.InvCheck;
import com.mes.inventory.domain.InvCheckDetail;
import com.mes.inventory.domain.InvIO;
import com.mes.inventory.domain.InvIODetail;
import com.mes.inventory.manager.InvInfoManager;
import com.mes.inventory.manager.iface.IInvInfoManager;

@Service
public class InvCheckService extends BasicService<InvCheck>{
	
	@Autowired
	private IInvCheckDao invCheckDao;
	
	@Autowired
	private IInvCheckDetailDao invCheckDetailDao;
	
	@Autowired
	private InvInfoManager invlnfoManager;
	
	//查询各个仓库的盘点单表头
	@DataProvider
	public Collection<InvCheck> getInvChecks(Map<String,Object> parameter) throws Exception{
  		if (null!=parameter) {
			String warehouseUid = (String) parameter.get("warehouseUid");
			List<InvCheck> list = invCheckDao.findByProperty("warehouseUid", warehouseUid);
			return list;
		}
		return null;
	}
	//查询各个仓库的盘点单表头带分页
	@DataProvider
	public void getInvCheck(Page<InvCheck> page, Map<String, Object> parameter){
		if (parameter != null && !parameter.equals("") ){
    		try {
    			invCheckDao.getInvCheck(page, parameter);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
	}
	//查询各个仓库的盘点单明细
		@DataProvider
		public void getInvCheckDetails(Page<InvCheckDetail> page,Map<String,Object> parameter) throws Exception{
			if (null!=parameter) {
				invCheckDetailDao.getInvCheckDetails(page, parameter);
			}
		}
	
	//测试
	@DataProvider
	public Collection<InvCheckDetail> getInvCheckDetails(String invcheckUid) throws Exception{
		
				List<InvCheckDetail> indes = invCheckDetailDao.findByProperty("invcheckUid", invcheckUid);
				return indes;
	}
	
	
	//保存盘点单
	@DataResolver
	public void saveInCheck(Collection<InvCheck>invChecks,Map<String,Object> parameter) throws Exception{
		String warehouseUid = (String)parameter.get("warehouseUid");
		for(InvCheck invCheck : invChecks){
			invCheck.setWarehouseUid(warehouseUid);
			invCheckDao.add(invCheck);
		}
	}
	//盘点登账
	@DataResolver
	public void saveInvoCheck(InvCheck invCheck, Collection<InvCheckDetail>invCheckDetails) throws Exception{
		if(invCheck.getInvCheckState()==5){
			throw new Exception("该盘点单已登帐！");
		}
			HashSet<InvCheckDetail> details = new HashSet<InvCheckDetail>();
			for(InvCheckDetail invCheckDetail : invCheckDetails){
				details.add(invCheckDetail);
			}
			invCheck.setInvCheckDetails(details);
		invlnfoManager.updateInvInfoByInvCheck(invCheck);
	}
	
	
	
		@DataResolver
		public void saveInCheckDetail(Collection<InvCheck>invChecks,Collection<InvCheckDetail>invCheckDetails) throws Exception{
			this.saveData(invChecks);
			int i = 1;
			for (InvCheckDetail invCheckDetail : invCheckDetails) {
				invCheckDetail.setSeq(i);
				i++;
			}
			this.saveData(invCheckDetails);
		}
}
